<?php

namespace App\Jobs;

use App\Models\Book;
use App\Service\BqsService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ImportBookChapter implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public Book $record;
    /**
     * Create a new job instance.
     */
    public function __construct(Book $book)
    {
        $this->record = $book;
    }


    /**
     * Execute the job.
     */
    public function handle(): void
    {
        //
        $list = BqsService::searchByName($this->record->title);
        foreach ($list as $item) {
            $this->record->chapters()->create([
                'chapter_title' => $item['name'],
                'chapter_link' => $item['url'],
            ]);
        }
    }
}
